<?php
/**
 * Author:   tangxiaowen
 * Email:    846506584@qq.com
 * Date:     2019/4/4 0004
 * Describe: 系统充值类
 */

namespace app\admin\controller;
use app\common\controller\order\Orderget;

class Charge extends Common
{
    private static $payStatus = ['1' => '已支付','2' => '未支付'];

    /**
     * 系统充值
     */
    public function charge()
    {
        if(request()->isAjax())
        {
            //获取请求的页数 条数 时间筛选参数
            $postData = Request()->only('Money,PayPic');
            $result = $this->validate($postData,'Charge.charge');
            if($result !== true){
                return json(['code' => 203,'msg' => $result,'data' => []]);
            }

            $temp = [
                'UserID'     => session('id'),             //用户ID
                'Money'      => $postData['Money'],        //金额
                'PayPic'     => $postData['PayPic'],       //支付凭证(截图)
                'PayStatus'  => 1,                         //状态 1已支付
                'CreateTime' => date('Y-m-d H:i:s',time()),//时间
            ];

            $addRes = db('sys_charge')->insert($temp);
            if($addRes){
                return json(['code' => 200,'msg' => '充值成功','data' => []]);
            }else{
                return json(['code' => 205,'msg' => '充值失败','data' => []]);
            }

        }

        return view();
    }

    /**
     * 获取充值记录
	 *
     */
    public function getChargeList()
    {
        if(request()->isAjax())
        {
           $page = input('page');//分页
		   $limit = input('limit');//显示个数
		   $Orderget=new Orderget();
	       $data=$Orderget->sorch();
           $where['c.Status'] = 1;
           if(!empty($data['time'])){
            $where['c.CreateTime'] = $data['time'];
		   }
            $data = db('sys_charge')->alias('c')
                ->join('xb_sys_administrator a','c.UserID = a.ID','LEFT')
                ->where($where)
                ->field('c.*,a.TrueName')
                ->page($page,$limit)
                ->group('c.ID')
                ->select();
            foreach ($data as $k=>$v){
                $data[$k]['PayStatus'] = self::$payStatus[$v['PayStatus']];
                $data[$k]['PayPic'] = getImgUrl($v['PayPic']);
            }
			$count = db('sys_charge')->alias('c')
                ->join('xb_sys_administrator a','c.UserID = a.ID','LEFT')
                ->where($where)
                ->count('a.ID');
            if($data){
                songsong($data,$count);  
	        }else{
	            songsong();
	        }
        }
        return view();
    }

}